package com.zoo.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.zoo.boot.bean.RoleEntity;
import com.zoo.boot.dao.RoleDao;
import com.zoo.boot.mapper.LRoleMapper;
import com.zoo.boot.mapper.LRoleMenuMapper;
import com.zoo.boot.mapper.LRoleUserMapper;
import com.zoo.boot.model.*;
import com.zoo.boot.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 角色管理 Service
 * @Author: WangB
 * @Date: 2021/6/25
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private LRoleUserMapper  roleUserMapper;

    @Resource
    private LRoleMapper roleMapper;

    @Resource
    private LRoleMenuMapper roleMenuMapper;

    @Resource
    private RoleDao roleDao;


    /**
     *  根据用户id 获取角色信息
     * @param userId
     * @return
     */
    @Override
    public LRole getRoleByUserId(Integer userId) {
        LRoleUserExample lRoleUserExample = new LRoleUserExample();
        lRoleUserExample.createCriteria().andUserIdEqualTo(userId);
        List<LRoleUser> lRoleUsers = roleUserMapper.selectByExample(lRoleUserExample);
        LRole  role = null;
        if(ObjectUtil.isNotEmpty(lRoleUsers)&& lRoleUsers.size()== 1 ){
            LRoleUser lRoleUser = lRoleUsers.get(0);
            role = roleMapper.selectByPrimaryKey(lRoleUser.getRoleId());
        }

        return role;
    }

    /**
     * 查询所有的角色信息
     * @return
     */
    @Override
    public List<LRole> getRoles() {
        return roleMapper.selectByExample(new LRoleExample());
    }


    /**
     * 根据查询条件查询角色信息
     * @param role
     * @return
     */
    @Override
    public List<LRole> getRoles(LRole role) {
        LRoleExample lRoleExample = new LRoleExample();
        // select *　from  l_role  where  role_name like %roleName%  and  create_time >= createTime
        if(StrUtil.isNotEmpty(role.getRoleName())){
            lRoleExample.createCriteria().andRoleNameLike("%"+role.getRoleName()+"%");
        }
        return roleMapper.selectByExample(lRoleExample);

    }


    /**
     * 添加角色信息，同时维护角色对应的菜单信息
     * @param roleEntity
     * @return
     */
    @Transactional
    @Override
    public boolean insertRole(RoleEntity roleEntity) {
        LRole lRole = new LRole();
        BeanUtil.copyProperties(roleEntity,lRole);
        lRole.setCreateTime(new Date());
        lRole.setUpdateTime(new Date());

        int insert = roleMapper.insert(lRole);
        boolean b = roleDao.insertRoleMenuBatch(lRole.getRoleId(), roleEntity.getMenuIds());
        return insert>0 && b;
    }


    /**
     * 角色信息修改，同时更新菜单关联信息
     * @param roleEntity
     * @return
     */
    @Transactional
    @Override
    public boolean update(RoleEntity roleEntity) {
        LRole lRole = new LRole();
        BeanUtil.copyProperties(roleEntity,lRole);
        lRole.setUpdateTime(new Date());
        //按照条件修改角色信息
        int i = roleMapper.updateByPrimaryKeySelective(lRole);
        LRoleMenuExample lRoleMenuExample = new LRoleMenuExample();
        lRoleMenuExample.createCriteria().andRoleIdEqualTo(roleEntity.getRoleId());
        //删除关联表相关角色的菜单
        int i1 = roleMenuMapper.deleteByExample(lRoleMenuExample);
        boolean b = roleDao.insertRoleMenuBatch(roleEntity.getRoleId(), roleEntity.getMenuIds());
        return i+i1 > 1 && b;
    }

    /**
     * 根据角色id 删除用户角色
     * @param roleId
     * @return
     */
    @Transactional
    @Override
    public boolean delete(Integer roleId) {
        int i = roleMapper.deleteByPrimaryKey(roleId);
        LRoleMenuExample lRoleMenuExample = new LRoleMenuExample();
        lRoleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        int i1 = roleMenuMapper.deleteByExample(lRoleMenuExample);
        return i+i1 > 1;
    }


    /**
     *  根据角色id 获取角色信息
     * @param roleId
     * @return
     */
    @Override
    public LRole getRoleByRoleId(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }
}
